package com.jl.controller;

import com.jl.commons.base.BaseController;
import com.jl.commons.constant.Constants;
import com.jl.commons.result.PageDataInfo;
import com.jl.commons.result.ResultData;
import com.jl.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author JiangJian
 */
@Controller
@RequestMapping("/common")
public class CommonController extends BaseController {

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private IDevicePartService devicePartService;

    @Autowired
    private IDamageComponentService damageComponentService;

    @Autowired
    private IDamagePartService damagePartService;


    @Autowired
    private IKeyWordService keyWordService;
    /**
     * 跳转到统计图表页面
     * @return
     */
    @GetMapping("/statisticalChart")
    @RequiresPermissions("/common/statisticalChart")
    public String toStatisticalChartPage(){
        return "statistical_chart";
    }

    /**
     * 故障时间最长的设备
     * @param type 1 周 2 月
     * @param startTime 起始时间
     * @param endTime 结束时间
     * @return 设备信息
     */
    @PostMapping("/deviceFaultSumTimeChart")
    @RequiresPermissions("/common/statisticalChart")
    @ResponseBody
    public ResultData getDeviceFaultTimeChart(Integer type,String startTime,String endTime){
        PageDataInfo<Map<String,Object>> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setCurrent(Constants.FIRST_PAGE);
        pageDataInfo.setSize(Constants.MAX_PAGE_SIZE);
        deviceService.getTopFaultTimeDevice(pageDataInfo,type,startTime,endTime);
        return success(pageDataInfo);
    }


    /**
     * 故障次数最多的设备
     * @param type 1 周 2 月
     * @param startTime 起始时间
     * @param endTime 结束时间
     * @return 设备信息
     */
    @PostMapping("/deviceFaultCountChart")
    @RequiresPermissions("/common/statisticalChart")
    @ResponseBody
    public ResultData getDeviceFaultCountChart(Integer type,String startTime,String endTime){
        PageDataInfo<Map<String,Object>> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setCurrent(Constants.FIRST_PAGE);
        pageDataInfo.setSize(Constants.MAX_PAGE_SIZE);
        deviceService.getTopFaultCountDevice(pageDataInfo,type,startTime,endTime);
        return success(pageDataInfo);
    }

    /**
     * 获取超过故障时间超过两个小时的次数
     * @param type 1 周 2 月
     * @param startTime 起始时间
     * @param endTime 结束时间
     * @return 设备信息
     */
    @PostMapping("/deviceFaultOverTwoCountChart")
    @RequiresPermissions("/common/statisticalChart")
    @ResponseBody
    public ResultData getDeviceFaultOverTwoCountChart(Integer type,String startTime,String endTime){
        PageDataInfo<Map<String,Object>> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setCurrent(Constants.FIRST_PAGE);
        pageDataInfo.setSize(Constants.MAX_PAGE_SIZE);
        deviceService.getTopOverHourCountDevice(pageDataInfo,type,startTime,endTime);
        return success(pageDataInfo);
    }


    /**
     * 发生故障次数最多的部位
     * @param type 1 周 2 月
     * @param startTime 起始时间
     * @param endTime 结束时间
     */
    @PostMapping("/damagePartCountChart")
    @RequiresPermissions("/common/statisticalChart")
    @ResponseBody
    public ResultData getDevicePartChart(Integer type,String startTime,String endTime){
        PageDataInfo<Map<String,Object>> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setCurrent(Constants.FIRST_PAGE);
        pageDataInfo.setSize(Constants.MAX_PAGE_SIZE);
        damagePartService.getTopDamagePartCount(pageDataInfo,type,startTime,endTime);
        return success(pageDataInfo);
    }

    /**
     * 更换最多的配件
     * @param type 1 周 2 月
     * @param startTime 起始时间
     * @param endTime 结束时间
     */
    @PostMapping("/damageComponentCountChart")
    @RequiresPermissions("/common/statisticalChart")
    @ResponseBody
    public ResultData getComponentChart(Integer type,String startTime,String endTime){
        PageDataInfo<Map<String,Object>> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setCurrent(Constants.FIRST_PAGE);
        pageDataInfo.setSize(Constants.MAX_PAGE_SIZE);
        damageComponentService.getTopDamageComponent(pageDataInfo,type,startTime,endTime);
        return success(pageDataInfo);
    }

//    @PostMapping("/devicePartChangeComponentChart")
//    @RequiresPermissions("/common/statisticalChart")
//    @ResponseBody
//    public ResultData getDevicePartChangeComponentChart(Integer top,String startTime,String endTime){
//        PageDataInfo<Map<String,Object>> pageDataInfo = new PageDataInfo<>();
//        pageDataInfo.setCurrent(Constants.FIRST_PAGE);
//        pageDataInfo.setSize(Constants.MAX_PAGE_SIZE);
//        devicePartService.selectTopChangeComponentDevicePart(pageDataInfo,top,startTime,endTime);
//        return success(pageDataInfo);
//    }



//    @PostMapping("/keyWordChart")
//    @RequiresPermissions("/common/statisticalChart")
//    @ResponseBody
//    public ResultData getKeyWordChart(Integer top,String startTime,String endTime){
//        PageDataInfo<Map<String,Object>> pageDataInfo = new PageDataInfo<>();
//        pageDataInfo.setCurrent(Constants.FIRST_PAGE);
//        pageDataInfo.setSize(Constants.MAX_PAGE_SIZE);
//        keyWordService.selectTopKeyWord(pageDataInfo,top,startTime,endTime);
//        return success(pageDataInfo);
//    }

}
